package com.tenbit.tbw.ctl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tenbit.tbw.svc.SYS1010SVC;
import com.tenbit.tbw.vo.SYS1010VO;
import com.tobesoft.platform.PlatformRequest;
import com.tobesoft.platform.PlatformResponse;
import com.tobesoft.platform.data.ColumnInfo;
import com.tobesoft.platform.data.Dataset;
import com.tobesoft.platform.data.DatasetList;
import com.tobesoft.platform.data.VariableList;

@Controller
public class Miplatform02 {

	@Autowired
	private SYS1010SVC svc;
	
	private Logger log=Logger.getLogger(this.getClass());
	
	@RequestMapping("SYS1010M1.do")
	public void getCodeBookList(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		
		DatasetList inDatasetList = new DatasetList();
		DatasetList outDatasetList = new DatasetList();
		VariableList outVariableList = new VariableList();
		
		Map<String, Object> paramMap1=new HashMap<String, Object>();
		Map<String,Object> resultMap1= new HashMap<String,Object>();
		
		Map<String, Object> paramMap2=new HashMap<String, Object>();
		Map<String,Object> resultMap2= new HashMap<String,Object>();
		
		PlatformRequest inRequest = new PlatformRequest(request,"utf-8");
		inRequest.receiveData();
		
		inDatasetList=inRequest.getDatasetList();
		
		Dataset ds_search=inDatasetList.getDataset("ds_search");
		
		Dataset ds_search2=new Dataset("ds_search2");
		Dataset ds_code_type_list=new Dataset("ds_code_type_list","utf-8");
		Dataset ds_code_detail_list=new Dataset("ds_code_detail_list","utf-8");
		
		log.debug(ds_search);
		
		if(ds_search.getColumnAsString(0,"search_code")==null)
		{
				
			int row = ds_search.appendRow();
			
			ds_search.setColumn(row, "search_code","code_type");
			ds_search.setColumn(row, "search_name","ITEM");
			
			paramMap1.put("type","code");
			paramMap1.put("name","ITEM");
		} else{
			
			if(ds_search.getColumnAsString(0,"search_code").equals("code_name")){
				paramMap1.put("type","name");
			}else if(ds_search.getColumnAsString(0,"search_code").equals("code_type")){
				paramMap1.put("type","code");
			}
			
			paramMap1.put("name",ds_search.getColumnAsString(0,"search_name"));
		}
		resultMap1 =svc.getCodeTypeList(paramMap1);
		
		
		if(resultMap1.get("rows")!=null)
		{
			List<SYS1010VO> list1= (List<SYS1010VO>) resultMap1.get("rows");
			
			ds_code_type_list.addColumn("code_type",ColumnInfo.COLUMN_TYPE_STRING, 50);
			ds_code_type_list.addColumn("code_detail",ColumnInfo.COLUMN_TYPE_STRING, 50);
			ds_code_type_list.addColumn("code_name",ColumnInfo.COLUMN_TYPE_STRING, 50);
			ds_code_type_list.addColumn("sort_order",ColumnInfo.COLUMN_TYPE_INT, 20);
			
			if(list1.size()>0){
				ds_search2.addColumn("code_type", ColumnInfo.COLUMN_TYPE_STRING, 20);
				ds_search2.addColumn("code_name", ColumnInfo.COLUMN_TYPE_STRING, 20);
				
				int ro =ds_search2.appendRow();
				ds_search2.setColumn(ro, "code_type", list1.get(0).getCodeType());
				ds_search2.setColumn(ro, "code_name", list1.get(0).getCodeName());	
				
				paramMap2.put("codeType", ds_search2.getColumnAsString(0,"code_type"));
				paramMap2.put("codeName", ds_search2.getColumnAsString(0,"code_type"));
				
				resultMap2=svc.getCodeDetailList(paramMap2);
			}
			for(int i = 0; i<list1.size() ; i++)
			{
				int row = ds_code_type_list.appendRow();
				
				ds_code_type_list.setColumn(row, "code_type", list1.get(i).getCodeType());
				ds_code_type_list.setColumn(row, "code_name", list1.get(i).getCodeName());
				ds_code_type_list.setColumn(row, "code_detail", list1.get(i).getCodeDetail());
				ds_code_type_list.setColumn(row, "sort_order", list1.get(i).getSortOrder());
			}
			
		}
		
		
		
		if(resultMap2.get("rows")!=null)
		{
			List<SYS1010VO> list1= (List<SYS1010VO>) resultMap2.get("rows");
			
			ds_code_detail_list.addColumn("code_type",ColumnInfo.COLUMN_TYPE_STRING, 50);
			ds_code_detail_list.addColumn("code_detail",ColumnInfo.COLUMN_TYPE_STRING, 50);
			ds_code_detail_list.addColumn("code_name",ColumnInfo.COLUMN_TYPE_STRING, 50);
			ds_code_detail_list.addColumn("sort_order",ColumnInfo.COLUMN_TYPE_INT, 20);
			
			for(int i = 0; i<list1.size() ; i++)
			{
				int row = ds_code_detail_list.appendRow();
				
				ds_code_detail_list.setColumn(row, "code_type", list1.get(i).getCodeType());
				ds_code_detail_list.setColumn(row, "code_name", list1.get(i).getCodeName());
				ds_code_detail_list.setColumn(row, "code_detail", list1.get(i).getCodeDetail());
				ds_code_detail_list.setColumn(row, "sort_order", list1.get(i).getSortOrder());
			}
		}
		
		outDatasetList.addDataset(ds_search);
		outDatasetList.addDataset(ds_search2);
		outDatasetList.addDataset(ds_code_type_list);
		outDatasetList.addDataset(ds_code_detail_list);
		
		PlatformResponse outResponse = new PlatformResponse(response,PlatformRequest.XML,"utf-8");
		outResponse.sendData(outVariableList,outDatasetList);
		
	}
	
	@RequestMapping("SYS1011M1.do")
	public void getDetailList(HttpServletRequest request,HttpServletResponse response) throws Exception
	{

		Map<String,Object> resultMap= new HashMap<String,Object>();
		Map<String,Object> paramMap= new HashMap<String,Object>();
		
		DatasetList inDatasetList = new DatasetList();
		DatasetList outDatasetList = new DatasetList();
		VariableList inVariableList = new VariableList();
		VariableList outVariableList = new VariableList();
		
		PlatformRequest inRequest = new PlatformRequest(request,"utf-8");
		inRequest.receiveData();
		
		inVariableList=inRequest.getVariableList();
		
		Dataset ds_search2=new Dataset("ds_search2");
		Dataset ds_code_detail_list=new Dataset("ds_code_detail_list","utf-8");
		
		
		ds_search2.addColumn("code_type", ColumnInfo.COLUMN_TYPE_STRING, 20);
		ds_search2.addColumn("code_name", ColumnInfo.COLUMN_TYPE_STRING, 20);
		
		int ro =ds_search2.appendRow();
		ds_search2.setColumn(ro, "code_type", inVariableList.getValueAsString("code_type"));
		ds_search2.setColumn(ro, "code_name", inVariableList.getValueAsString("code_name"));
		
		
		paramMap.put("codeType", inVariableList.getValueAsString("code_type"));
		paramMap.put("codeName", inVariableList.getValueAsString("code_name"));
		
		resultMap =svc.getCodeDetailList(paramMap);
		
		if(resultMap.get("rows")!=null)
		{
			List<SYS1010VO> list1= (List<SYS1010VO>) resultMap.get("rows");
			
			ds_code_detail_list.addColumn("code_type",ColumnInfo.COLUMN_TYPE_STRING, 50);
			ds_code_detail_list.addColumn("code_detail",ColumnInfo.COLUMN_TYPE_STRING, 50);
			ds_code_detail_list.addColumn("code_name",ColumnInfo.COLUMN_TYPE_STRING, 50);
			ds_code_detail_list.addColumn("sort_order",ColumnInfo.COLUMN_TYPE_INT, 20);
			
			for(int i = 0; i<list1.size() ; i++)
			{
				int row = ds_code_detail_list.appendRow();
				
				ds_code_detail_list.setColumn(row, "code_type", list1.get(i).getCodeType());
				ds_code_detail_list.setColumn(row, "code_name", list1.get(i).getCodeName());
				ds_code_detail_list.setColumn(row, "code_detail", list1.get(i).getCodeDetail());
				ds_code_detail_list.setColumn(row, "sort_order", list1.get(i).getSortOrder());
			}
		}
		
		outDatasetList.addDataset(ds_search2);
		outDatasetList.addDataset(ds_code_detail_list);
		
		PlatformResponse outResponse = new PlatformResponse(response,PlatformRequest.XML,"utf-8");
		outResponse.sendData(outVariableList,outDatasetList);
	}
	
	@RequestMapping("SYS1012M1.do")
	public void setCodeTypelList(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		Map<String,Object> resultMap= new HashMap<String,Object>();
	
		
		DatasetList inDatasetList = new DatasetList();
		DatasetList outDatasetList = new DatasetList();
		VariableList inVariableList = new VariableList();
		VariableList outVariableList = new VariableList();
		
		PlatformRequest inRequest = new PlatformRequest(request,"utf-8");
		inRequest.receiveData();
		
		inDatasetList=inRequest.getDatasetList();
		
		Dataset ds_code_type_list=inDatasetList.getDataset("ds_code_type_list");
		
		List <Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		
		
		log.debug(ds_code_type_list.getDeleteRowCount());
		
		int index=0;
		
		if(ds_code_type_list.getDeleteRowCount()>0)
		{
			for(int i=0;i<ds_code_type_list.getDeleteRowCount();i++)
			{
				Map<String,Object> map= new HashMap<String,Object>();
				
				map.put("codeType",ds_code_type_list.getDeleteColumn(i, "code_type").toString());
				map.put("codeDetail",ds_code_type_list.getDeleteColumn(i, "code_detail").toString());
				map.put("codeName",ds_code_type_list.getDeleteColumn(i, "code_name").toString());
				map.put("sortOrder",ds_code_type_list.getDeleteColumn(i, "sort_order").toString());
		
				map.put("sign", "D");
				
				list.add(index,map);
				index++;
				
			}
			log.debug("del:++++++++++++++++++++++++++++++++++++++++++++++++"+list);
	
		}
		
		if(ds_code_type_list.getRowCount()>0)
		{
			for(int i=0;i<ds_code_type_list.getRowCount();i++)
			{
				Map<String,Object> map= new HashMap<String,Object>();
				
				if(ds_code_type_list.getRowStatus(i).equals("insert"))
				{
					if(ds_code_type_list.getColumnAsString(i,"code_type")==null){
						map.put("codeType", "");
						map.put("codeDetail", "");
					}else if(ds_code_type_list.getColumnAsString(i,"code_type")!=null){
						map.put("codeType", ds_code_type_list.getColumnAsString(i,"code_type"));
						map.put("codeDetail", ds_code_type_list.getColumnAsString(i,"code_type"));
					}
					if(ds_code_type_list.getColumnAsString(i,"code_name")==null){
						map.put("codeName", "");
					}else if(ds_code_type_list.getColumnAsString(i,"code_name")!=null){
						map.put("codeName", ds_code_type_list.getColumnAsString(i,"code_name"));
					}
					map.put("sortOrder", "0");
					map.put("sign", "I");
				} else if(ds_code_type_list.getRowStatus(i).equals("update"))
				{
					map.put("codeType", ds_code_type_list.getColumnAsString(i,"code_type"));
					map.put("codeDetail", ds_code_type_list.getColumnAsString(i,"code_detail"));
					map.put("codeName", ds_code_type_list.getColumnAsString(i,"code_name"));
					map.put("sortOrder", ds_code_type_list.getColumnAsString(i,"sort_order"));
					map.put("sign", "U");
				}
				
				list.add(index,map);
				index++;
				log.debug(list);
				
			}
		}
		
		log.debug("입력수정삭제 파라미터:"+list);
		resultMap=svc.setCodeBookInfo(list);
		PlatformResponse outResponse = new PlatformResponse(response,PlatformRequest.XML,"utf-8");
		
		if(!(Boolean) resultMap.get("result")){
			outVariableList.add("ErrorCode", -1);
			outVariableList.add("ErrorMsg", (String)resultMap.get("msg"));
			outResponse.sendData(outVariableList,outDatasetList);
			
		} else{
			outVariableList.add("ErrorCode", 0);
			outVariableList.add("ErrorMsg", (String)resultMap.get("msg"));
			outResponse.sendData(outVariableList,outDatasetList);
		}
		
		
	}
	@RequestMapping("SYS1013M1.do")
	public void setCodeDetailList(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		Map<String,Object> resultMap= new HashMap<String,Object>();
		Map<String,Object> paramMap= new HashMap<String,Object>();
		
		DatasetList inDatasetList = new DatasetList();
		DatasetList outDatasetList = new DatasetList();
		VariableList inVariableList = new VariableList();
		VariableList outVariableList = new VariableList();
		
		
		
		PlatformRequest inRequest = new PlatformRequest(request,"utf-8");
		inRequest.receiveData();
		
		inDatasetList=inRequest.getDatasetList();
		inVariableList=inRequest.getVariableList();
		
		
		
		Dataset ds_code_detail_list=inDatasetList.getDataset("ds_code_detail_list");
		
		
		
		
		List <Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		
		
		log.debug("왜 널이냐!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+ds_code_detail_list);
		
		int index=0;
		
		if(ds_code_detail_list.getDeleteRowCount()>0)
		{
			for(int i=0;i<ds_code_detail_list.getDeleteRowCount();i++)
			{
				Map<String,Object> map= new HashMap<String,Object>();
				
				map.put("codeType",ds_code_detail_list.getDeleteColumn(i, "code_type").toString());
				map.put("codeDetail",ds_code_detail_list.getDeleteColumn(i, "code_detail").toString());
				map.put("codeName",ds_code_detail_list.getDeleteColumn(i, "code_name").toString());
				map.put("sortOrder",ds_code_detail_list.getDeleteColumn(i, "sort_order").toString());
		
				map.put("sign", "D");
				
				list.add(index,map);
				index++;
				
			}
			log.debug("del:++++++++++++++++++++++++++++++++++++++++++++++++"+list);
	
		}
		
		if(ds_code_detail_list.getRowCount()>0)
		{
			for(int i=0;i<ds_code_detail_list.getRowCount();i++)
			{
				Map<String,Object> map= new HashMap<String,Object>();
				
				if(ds_code_detail_list.getRowStatus(i).equals("insert"))
				{
					if(ds_code_detail_list.getColumnAsString(i,"code_detail")==null){
						
						map.put("codeDetail", "");
						
					}else if(ds_code_detail_list.getColumnAsString(i,"code_detail")!=null){
						
						map.put("codeDetail", ds_code_detail_list.getColumnAsString(i,"code_detail"));
					}
					if(ds_code_detail_list.getColumnAsString(i,"code_name")==null){
						map.put("codeName", "");
					}else if(ds_code_detail_list.getColumnAsString(i,"code_name")!=null){
						map.put("codeName", ds_code_detail_list.getColumnAsString(i,"code_name"));
					}
					if(ds_code_detail_list.getColumnAsString(i,"sort_order")==null){
						map.put("sortOrder", "");
					}else if(ds_code_detail_list.getColumnAsString(i,"sort_order")!=null){
						map.put("sortOrder", ds_code_detail_list.getColumnAsString(i,"sort_order"));
					}
					map.put("codeType", ds_code_detail_list.getColumnAsString(i,"code_type"));
					
					map.put("sign", "I");
				} else if(ds_code_detail_list.getRowStatus(i).equals("update"))
				{
					map.put("codeType", ds_code_detail_list.getColumnAsString(i,"code_type"));
					map.put("codeDetail", ds_code_detail_list.getColumnAsString(i,"code_detail"));
					map.put("codeName", ds_code_detail_list.getColumnAsString(i,"code_name"));
					map.put("sortOrder", ds_code_detail_list.getColumnAsString(i,"sort_order"));
					map.put("sign", "U");
				}
				
				list.add(index,map);
				index++;
				log.debug("++++++++++++++++++++++++++++++++"+list);
				
			}
		}
		
		log.debug("입력수정삭제 파라미터:"+list);
		resultMap=svc.setCodeBookInfo(list);
		PlatformResponse outResponse = new PlatformResponse(response,PlatformRequest.XML,"utf-8");
		
		if(!(Boolean) resultMap.get("result")){
			outVariableList.add("ErrorCode", -1);
			outVariableList.add("ErrorMsg", (String)resultMap.get("msg"));
			outResponse.sendData(outVariableList,outDatasetList);
			
		} else{
			outVariableList.add("ErrorCode", 0);
			outVariableList.add("ErrorMsg", (String)resultMap.get("msg"));
			outResponse.sendData(outVariableList,outDatasetList);
		}
		
		
	}
}
